package droid.insane.GUI;

import java.util.ArrayList;

import droid.insane.database.DBHandler;
import droid.insane.logic.ConstantValues;
import droid.insane.logic.ExtendedListView;
import droid.insane.logic.PortfolioManager;
import droid.insane.logic.R;
import droid.insane.logic.UpdateFlags;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;

/**
 * @author Sandamal
 *
 */
public class TopTenWindow extends ListActivity {
	private static ArrayList<String[]> rowList;
	private ExtendedListView mAdapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.topten);

		rowList = getTopData();
		mAdapter = new ExtendedListView(this, rowList, R.layout.listentry);
		setListAdapter(mAdapter);
	}

	/**
	 * @return an Arraylist taht contains the data of the top companies
	 */
	private ArrayList<String[]> getTopData() {
		ArrayList<String[]> resultSet = null;
		// open the data base to read values
		DBHandler dbHandler = DBHandler.getDBHandler(null);
		try {
			dbHandler.openToRead();
			resultSet = dbHandler.getTopStocks();
			dbHandler.close();
		} catch (Exception e) {
			Log.e(ConstantValues.TAG, e.getMessage());
		}
		return resultSet;
	}

	/**
	 * Updates the display when the dataabse is updated
	 */
	private void updateDisplay() {
		mAdapter.updateDataset(getTopData());
	}

	@Override
	protected void onResume() {
		if (UpdateFlags.isTOP_TEN_FLAG()) {
			updateDisplay();
			UpdateFlags.setPORTFOLIO_FLAG(false);
		}
		super.onResume();
	}
}
